import { BaseDeployPanel } from "./BaseDeployPanel";
import { Language } from "../../../../../../extend/language/Language";
import { Cache } from "../../../../cache/Cache";
import { EActiveType } from "../../../../../../Message/Public/EActiveType";
import { ItemData } from "../../../../resource/info/ItemData";
import { ShopConfig } from "../../../../resource/ShopConfig";
import { Dispatcher } from "../../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../../events/DataEvent";
import { EventName } from "../../../../mvc/EventName";
import { NetDispatcher } from "../../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../../mvc/ServerCommand";
import { EBind } from "../../../../../../Message/Public/EBind";
import { GlobalClass } from "../../../../../../com/mui/core/GlobalClass";
import { ItemConfig } from "../../../../resource/ItemConfig";
import { JSON } from "../../../../../../com/mui/serialization/json/JSON";
import { EquipmentModuleConst } from "../../data/EquipmentModuleConst";
import { EquipAdvanceConfig } from "../../../../resource/EquipAdvanceConfig";
import { EColor } from "../../../../../../Message/Public/EColor";
import { ECategory } from "../../../../../../Message/Public/ECategory";
import { MsgManager } from "../../../../manager/MsgManager";
import { EquipmentUtil } from "../../../common/util/EquipmentUtil";
import { EItemUpdateResult } from "../../../../../../Message/Game/EItemUpdateResult";
import { DeployPanelItem } from "../DeployPanelItem";
import { FilterConst } from "../../../../../component/gconst/FilterConst";
import { EmbedSpecialJewelInfo } from "../EmbedSpecialJewelInfo";
import { Alert } from "../../../../../../com/mui/controls/Alert";
import { EquipUpdateParams } from "../../data/EquipUpdateParams";
import { _label456 } from "../../../../../../this/_label456";
type int = number;
//class EmbedJewelPanel
    
    export  class EmbedJewelPanel extends BaseDeployPanel
    {
        constructor()
        {
            this._label456 = Language.getString(60145);
            this.posData = [{"x": 157, "y": 110, "label": Language.getString(60144), "canRemove": true, "noBoarder": false, "tooltip":""}, {"x": 33, "y": 180, "label":"", "canRemove": true, "noBoarder": true, "isJewel": true, "tooltip":""}, {"x": 82, "y": 180, "label":"", "canRemove": true, "noBoarder": true, "isJewel": true, "tooltip":""}, {"x": 131, "y": 180, "label":"", "canRemove": true, "noBoarder": true, "isJewel": true, "tooltip":""}, {"x": 180, "y": 180, "label":"", "canRemove": true, "noBoarder": true, "isJewel": true, "tooltip": Language.getString(61029)}, {"x": 229, "y": 180, "label":"", "canRemove": true, "noBoarder": true, "isJewel": true, "tooltip": Language.getString(90289)}, {"x": 278, "y": 180, "label":"", "canRemove": true, "noBoarder": true, "isJewel": true, "tooltip": Language.getString(74102)}, {"x": 33, "y": 230, "label": _label456, "canRemove": false, "noBoarder": true, "showName": true, "tooltip":""}, {"x": 82, "y": 230, "label": _label456, "canRemove": false, "noBoarder": true, "showName": true, "tooltip":""}, {"x": 131, "y": 230, "label": _label456, "canRemove": false, "noBoarder": true, "showName": true, "tooltip":""}, {"x": 180, "y": 230, "label": _label456, "canRemove": false, "noBoarder": true, "showName": true, "tooltip":""}, {"x": 229, "y": 230, "label": _label456, "canRemove": false, "noBoarder": true, "showName": true, "tooltip":""}, {"x": 278, "y": 230, "label": _label456, "canRemove": false, "noBoarder": true, "showName": true, "tooltip":""}];
            this.buttonLabel = Language.getString(60146);
            this._alarmText = Language.getString(60147);
            super();
            return;
        }

        /* internal  */isInFreeEmbedActiveTime(): Boolean
        {
            return Cache.instance.active.isActiveInEffect(EActiveType._EActiveTypeFreeEmbedJewel);
        }

        /* internal  */autoBuyMaterial(arg1: ItemData): void
        {
            var loc1=ShopConfig.instance.getShopItemInfoByCode(arg1.itemCode);
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItem, {"shopItemInfo": loc1, "amount": 1, "callBack": this.onItemBuySuccess}));
            return;
        }

        /* internal  */onItemBuySuccess(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            return;
        }

        /* internal  */serverBagDataChangehandler(arg1): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            this.sendMateriaDataToController();
            this.onConfirm();
            return;
        }

        /* internal  */checkIsChangeToBind(): Boolean
        {
            var loc1=false;
            var loc2=1;
            while (loc2 <= 6) 
            {
                if (this["hole" + loc2] == 1 && allItem[loc2].itemData && allItem[loc2].enabled && allItem[loc2].itemData.itemInfo.item.bind == EBind._EBindYes || this["hole" + loc2] == 1 && allItem[loc2 + 6].itemData && allItem[loc2 + 6].enabled && allItem[loc2 + 6].itemData.itemInfo.item.bind == EBind._EBindYes) 
                {
                    loc1 = true;
                    break;
                }
                ++loc2;
            }
            if (!(allItem[0].itemData.itemExInfo.bind == EBind._EBindYes) && loc1) 
            {
                return true;
            }
            return false;
        }

        /* internal  */updateHoles(): void
        {
            var loc1=0;
            var loc3=0;
            var loc2=allItem[0].itemData;
            if (loc2) 
            {
                loc1 = 1;
                while (loc1 <= 6) 
                {
                    loc3 = loc2.itemExInfo["hole" + loc1];
                    this["hole" + loc1] = loc3;
                    if (loc3 == 0) 
                    {
                        allItem[loc1].itemData = null;
                        allItem[loc1].selected = false;
                        allItem[loc1].source = GlobalClass.getBitmap("NoHole");
                    }
                    else if (loc3 == 1) 
                    {
                        allItem[loc1].itemData = null;
                        allItem[loc1].source = null;
                        allItem[loc1].enabled = true;
                        allItem[loc1 + 6].enabled = true;
                    }
                    else 
                    {
                        allItem[loc1].itemData = new ItemData(loc3);
                        allItem[loc1].enabled = false;
                    }
                    ++loc1;
                }
            }
            else 
            {
                loc1 = 1;
                while (loc1 <= 6) 
                {
                    allItem[loc1].itemData = null;
                    ++loc1;
                }
            }
            return;
        }

        protected /* override */ updateStatus(): void
        {
            var loc4=null;
            if (!this.updateConfigInfo) 
            {
                loc4 = ItemConfig.instance.getUpdateInfo(ItemConfig.EmbedJewel, 1)["condition"];
                this.updateConfigInfo = JSON.deserialize(loc4);
            }
            this.updateSpecialInfo();
            var loc1=0;
            var loc2=0;
            var loc3=1;
            while (loc3 <= 6) 
            {
                if (this["hole" + loc3] == 1 && allItem[loc3].itemData) 
                {
                    ++loc1;
                }
                ++loc3;
            }
            if (this.updateConfigInfo) 
            {
                loc2 = loc1 * this.updateConfigInfo.cost;
            }
            else 
            {
                loc2 = 0;
            }
            if (_cost != loc2) 
            {
                _cost = loc2;
                dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CostChange, _cost, false, true));
            }
            confirmButton.enabled = true;
            return;
        }

        /* internal  */updateSpecialInfo(): void
        {
            var loc2=null;
            var loc1=allItem[0].itemData;
            if (loc1) 
            {
                this.addChild(this.infoPanel);
                loc2 = EquipAdvanceConfig.instance.getJewelEmbedPosInfo(loc1.category, loc1.itemInfo.type);
                if (loc2) 
                {
                    if (loc1.color <= EColor._EColorRed) 
                    {
                        this.infoPanel.setJewelTypeString(loc2.desc1, loc2.desc2, "", loc1);
                    }
                    else 
                    {
                        this.infoPanel.setJewelTypeString(loc2.desc1, loc2.desc2, loc2.desc3, loc1);
                    }
                }
                else 
                {
                    this.infoPanel.setJewelTypeString("", "", "", null);
                }
            }
            else if (this.infoPanel.parent) 
            {
                this.infoPanel.parent.removeChild(this.infoPanel);
            }
            return;
        }

        /* internal  */getCanPutPos(arg1: ItemData): Array<any>
        {
            var loc2=0;
            var loc1=[];
            var loc3=1;
            while (loc3 <= 6) 
            {
                if (this["hole" + loc3] == 1 && allItem[loc3].itemData && allItem[loc3].itemData.itemInfo.item.itemLevel < arg1.itemInfo.item["effectEx"]) 
                {
                    if (!allItem[loc3 + 6].itemData || !allItem[loc3 + 6].enabled) 
                    {
                        return [loc3 + 6];
                    }
                    loc1.push(loc3 + 6);
                }
                ++loc3;
            }
            return loc1;
        }

        public /* override */ putInItem(arg1: ItemData, arg2: Boolean=false, arg3=null): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            var loc4=0;
            if (arg1.itemInfo.item.category == ECategory._ECategoryEquip || arg1.itemInfo.item.category == ECategory._ECategoryPetEquip) 
            {
                this.clear();
                allItem[0].itemData = arg1;
                this.currentEquipmentData = arg1;
                this.updateHoles();
                this.updateStatus();
                this.updateTips();
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [arg1], false, true));
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_MaterialTypeChange, null, false, true));
            }
            else if (arg1.itemInfo.item.category == ECategory._ECategoryJewel) 
            {
                if (!(loc1 = allItem[0].itemData)) 
                {
                    return;
                }
                if (!(loc1.itemExInfo.hole1 == 1) && !(loc1.itemExInfo.hole2 == 1) && !(loc1.itemExInfo.hole3 == 1) && !(loc1.itemExInfo.hole4 == 1) && !(loc1.itemExInfo.hole5 == 1) && !(loc1.itemExInfo.hole6 == 1)) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(60150));
                    return;
                }
                loc4 = 0;
                loc3 = 1;
                while (loc3 <= 6) 
                {
                    if ((loc2 = allItem[loc3].itemData) && arg1.type == loc2.type) 
                    {
                        if (arg1.uid == loc2.uid) 
                        {
                            return;
                        }
                        if (this["hole" + loc3] != 1) 
                        {
                            MsgManager.showRollTipsMsg(Language.getString(60151));
                            return;
                        }
                        else 
                        {
                            loc4 = loc3;
                            break;
                        }
                    }
                    ++loc3;
                }
                if (loc4 == 0) 
                {
                    loc3 = 1;
                    while (loc3 <= 6) 
                    {
                        if (!(loc2 = allItem[loc3].itemData) && this["hole" + loc3] == 1) 
                        {
                            loc4 = loc3;
                        }
                        ++loc3;
                    }
                }
                if (loc4 == 0) 
                {
                    loc3 = 0;
                    while (loc3 < 6) 
                    {
                        loc4 = (this.lastPutInJewelPos + loc3) % 5 + 1;
                        if (this["hole" + loc4] != 1) 
                        {
                        };
                        ++loc3;
                    }
                }
                if (loc4 == 6) 
                {
                    if (!(loc1.color >= EColor._EColorRedGold && loc1.itemExInfo.hole6 == 1 && EquipmentUtil.isSuitableJewel(arg1, loc1, 6))) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(74015));
                        return;
                    }
                }
                else if (EquipmentUtil.isSuitableJewel(arg1, loc1, loc4)) 
                {
                    if (this["hole" + loc4] != 1) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(60150));
                        return;
                    }
                }
                else if (loc1.color >= EColor._EColorRedGold && loc1.itemExInfo.hole6 == 1 && EquipmentUtil.isSuitableJewel(arg1, loc1, 6)) 
                {
                    loc4 = 6;
                }
                else 
                {
                    MsgManager.showRollTipsMsg(Language.getString(61028));
                    return;
                }
                allItem[loc4].itemData = arg1;
                this.lastPutInJewelPos = loc4;
                this.updateStatus();
            }
            this.sendMateriaDataToController();
            return;
        }

        /* internal  */updateTips(): void
        {
            var loc1=allItem[0].itemData;
            if (loc1 && loc1.category == ECategory._ECategoryPetEquip) 
            {
                allItem[4].toolTipData = Language.getString(61360);
                allItem[5].toolTipData = Language.getString(61361);
                allItem[6].toolTipData = Language.getString(79003);
            }
            else if (loc1 && loc1.category == ECategory._ECategoryEquip) 
            {
                allItem[4].toolTipData = Language.getString(61029);
                allItem[5].toolTipData = Language.getString(90289);
                allItem[6].toolTipData = Language.getString(74102);
            }
            else 
            {
                allItem[4].toolTipData = "";
                allItem[5].toolTipData = "";
                allItem[6].toolTipData = "";
            }
            return;
        }

        /* internal  */sendMateriaDataToController(): void
        {
            var loc1=[{"jewelData": this.hole1 != 1 ? null : allItem[1].itemData, "materialData": allItem[6].itemData, "isEnough": true}, {"jewelData": this.hole2 != 1 ? null : allItem[2].itemData, "materialData": allItem[7].itemData, "isEnough": true}, {"jewelData": this.hole3 != 1 ? null : allItem[3].itemData, "materialData": allItem[8].itemData, "isEnough": true}, {"jewelData": this.hole4 != 1 ? null : allItem[4].itemData, "materialData": allItem[9].itemData, "isEnough": true}, {"jewelData": this.hole5 != 1 ? null : allItem[5].itemData, "materialData": allItem[10].itemData, "isEnough": true}, {"jewelData": this.hole6 != 1 ? null : allItem[6].itemData, "materialData": allItem[11].itemData, "isEnough": true}];
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedMaterialChange, loc1, false, true));
            return;
        }

        public moveItemOutByIndex(arg1: int): void
        {
            if (arg1 != 0) 
            {
                if (arg1 == 1 && this.hole1 == 1) 
                {
                    allItem[1].itemData = null;
                    allItem[7].itemData = null;
                    allItem[7].enabled = true;
                }
                else if (arg1 == 2 && this.hole2 == 1) 
                {
                    allItem[2].itemData = null;
                    allItem[8].itemData = null;
                    allItem[8].enabled = true;
                }
                else if (arg1 == 3 && this.hole3 == 1) 
                {
                    allItem[3].itemData = null;
                    allItem[9].itemData = null;
                    allItem[9].enabled = true;
                }
                else if (arg1 == 4 && this.hole4 == 1) 
                {
                    allItem[4].itemData = null;
                    allItem[10].itemData = null;
                    allItem[10].enabled = true;
                }
                else if (arg1 == 5 && this.hole5 == 1) 
                {
                    allItem[5].itemData = null;
                    allItem[11].itemData = null;
                    allItem[11].enabled = true;
                }
                else if (arg1 == 6 && this.hole6 == 1) 
                {
                    allItem[6].itemData = null;
                    allItem[12].itemData = null;
                    allItem[12].enabled = true;
                }
                else if (arg1 == 7 || arg1 == 8 || arg1 == 9 || arg1 == 10 || arg1 == 11) 
                {
                    allItem[arg1].itemData = null;
                    allItem[arg1].enabled = false;
                }
            }
            else 
            {
                this.clear();
                this.updateHoles();
                this.updateTips();
            }
            this.sendMateriaDataToController();
            this.updateStatus();
            return;
        }

        public /* override */ clear(): void
        {
            clearAllItem();
            allItem[1].selected = false;
            allItem[2].selected = false;
            allItem[3].selected = false;
            allItem[4].selected = false;
            allItem[5].selected = false;
            allItem[6].selected = false;
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [], false, true));
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_MaterialTypeChange, null, false, true));
            return;
        }

        public /* override */ setAutoChooseMaterial(arg1: Object): void
        {
            var loc1=arg1.material as Array<any>;
            var loc2=0;
            while (loc2 < loc1.length) 
            {
                allItem[7 + loc2].itemData = loc1[loc2].materialData;
                allItem[7 + loc2].enabled = loc1[loc2].isEnough;
                ++loc2;
            }
            this.updateStatus();
            return;
        }

        public /* override */ onUpdateResult(arg1: int, arg2: Boolean, arg3, arg4: Array<any>): void
        {
            if (arg1 != EItemUpdateResult._EItemUpdateResultSuccess) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60153));
                this.clear();
            }
            else 
            {
                this.clear();
                MsgManager.showRollTipsMsg(Language.getString(60152));
                addUpdateSuccessEffect("equipEffect_embed.swf", 177, 145);
            }
            this.putInItem(new ItemData(arg3));
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc2=null;
            var loc3=null;
            super.createChildren();
            var loc1=0;
            while (loc1 < this.posData.length) 
            {
                loc2 = this.posData[loc1];
                loc3 = new DeployPanelItem(loc1, loc2.noBoarder, loc2.isJewel, loc2.tooltip, loc2.showName);
                loc3.posIndex = loc1;
                loc3.x = loc2.x;
                loc3.y = loc2.y;
                loc3.label = loc2.label;
                if (loc2.canRemove) 
                {
                    loc3.mouseEnabled = true;
                    loc3.addEventListener(flash.events.MouseEvent.CLICK, this.onItemDoubleClick);
                }
                addChild(loc3);
                allItem.push(loc3);
                ++loc1;
            }
            confirmButton.label = this.buttonLabel;
            confirmButton.x = 143;
            confirmButton.y = 297;
            confirmButton.textField.filters = [FilterConst.glowFilter];
            confirmButton.styleName = "ButtonSpecial";
            addChild(confirmButton);
            confirmButton.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            this.infoPanel = new EmbedSpecialJewelInfo();
            this.infoPanel.x = 210;
            this.infoPanel.y = 90;
            this.updateHoles();
            return;
        }

        /* internal  */onItemDoubleClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as DeployPanelItem;
            if (loc1.itemData) 
            {
                this.moveItemOutByIndex(loc1.posIndex);
            }
            return;
        }

        /* internal  */onButtonClick(arg1: Object /* flash.events.MouseEvent */=null): void
        {
            var e: Object /* flash.events.MouseEvent */=null;
            var closeAlert: Function;

            var loc1;
            e = arg1;
            if (allItem[0].itemData == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60148));
                return;
            }
            if (this.checkIsChangeToBind()) 
            {
                closeAlert = (arg1: int): void
                {
                    if (arg1 == Alert.OK) 
                    {
                        onConfirm();
                    }
                    return;
                }
                Alert.show(this._alarmText, null, Alert.OK | Alert.CANCEL, null, closeAlert);
            }
            else 
            {
                this.onConfirm();
            }
            return;
        }

        /* internal  */onConfirm(): void
        {
            var loc6=0;
            if (!allItem[0].itemData) 
            {
                return;
            }
            var loc1=this.currentEquipmentData.uid;
            var loc2=this.currentEquipmentData.posType;
            var loc3=[];
            loc3.push(allItem[0].itemData.uid);
            var loc4=1;
            while (loc4 <= 6) 
            {
                loc6 = loc4 + 6;
                if (allItem[loc4].itemData && this["hole" + loc4] == 1 && allItem[loc6].itemData && (allItem[loc6].enabled || this.isInFreeEmbedActiveTime())) 
                {
                    loc3.push(allItem[loc4].itemData.uid);
                    loc3.push(allItem[loc6].itemData.uid);
                }
                else 
                {
                    if (allItem[loc4].itemData && this["hole" + loc4] == 1 && allItem[loc6].itemData && !allItem[loc6].enabled && Cache.instance.equipment.isAutoBuy) 
                    {
                        this.autoBuyMaterial(allItem[loc6].itemData);
                        return;
                    }
                    if (allItem[loc4].itemData && this["hole" + loc4] == 1 && allItem[loc6].itemData && !allItem[loc6].enabled) 
                    {
                        this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChangeMaterialTabBarIndex, 0, false, true, true));
                    }
                }
                ++loc4;
            }
            if (loc3.length == 1) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60149));
                return;
            }
            if (!isEnoughMoney) 
            {
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CoinNotEnough, null, false, true, true));
                MsgManager.showRollTipsMsg(Language.getString(60121));
                return;
            }
            confirmButton.enabled = false;
            var loc5;
            (loc5 = new EquipUpdateParams()).operationId = this.operationId;
            loc5.autoUpdate = false;
            loc5.equipPosType = loc2;
            loc5.materials = loc3;
            loc5.autoBuyTone = Cache.instance.equipment.isAutoBuy;
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_DoUpdateEvent1, loc5, false, true));
            return;
        }

        public /* var */currentEquipmentData: ItemData;

        /* internal */ /* var */operationId: int=3;

        /* internal */ /* var */lastPutInJewelPos: int=0;

        /* internal */ /* var */lastPutInPropPos: int=0;

        /* internal */ /* var */_label456: string;

        /* internal */ /* var */posData: Array<any>;

        /* internal */ /* var */buttonLabel: string;

        /* internal */ /* var */_alarmText: string;

        /* internal */ /* var */hole1: int;

        /* internal */ /* var */hole2: int;

        /* internal */ /* var */hole3: int;

        /* internal */ /* var */hole4: int;

        /* internal */ /* var */hole5: int;

        /* internal */ /* var */hole6: int;

        /* internal */ /* var */updateConfigInfo: Object;

        /* internal */ /* var */infoPanel: EmbedSpecialJewelInfo;
    }
